﻿using AutoMapper;
using D.B.Data.API.Applications.Command;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.B.Data.API.Applications.CommandHandler
{
    public class QueryUserCommandHandler : IRequestHandler<QueryUserCommand, APIPaging<UserDTO>>
    {
        private readonly IBaseRepository<UserModel> baseRepository;
        private readonly IBaseRepository<AddressModel> AddressRepository;
        private readonly IBaseRepository<AccountModel> AccountRepository;
        private readonly IBaseRepository<UserRoleModel> userroleRepository;
        private readonly IBaseRepository<RoleModel> roleRepository;
        private readonly IMapper mapper;

        public QueryUserCommandHandler(IBaseRepository<UserModel> baseRepository, IMapper mapper, IBaseRepository<AddressModel> addressRepository, IBaseRepository<AccountModel> accountRepository, IBaseRepository<UserRoleModel> userroleRepository, IBaseRepository<RoleModel> roleRepository)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
            AddressRepository = addressRepository;
            AccountRepository = accountRepository;
            this.userroleRepository = userroleRepository;
            this.roleRepository = roleRepository;
        }

        public Task<APIPaging<UserDTO>> Handle(QueryUserCommand request, CancellationToken cancellationToken)
        {
            var res = baseRepository.GetAll().AsNoTracking().Where(s => !s.IsDel);
            var Address = AddressRepository.GetAll().AsNoTracking().Where(s => !s.IsDel);

            var model = mapper.Map<List<UserDTO>>(res);

            foreach (var item in model)
            {
                item.CreateUserName = res.FirstOrDefault(s => s.Id == item.CreateUserId)!.UserName;
                if(item.UpDataUserId == 0)
                {
                    item.UpdateUserName = "";
                }
                else
                {
                    item.UpdateUserName = res.FirstOrDefault(s => s.Id == item.UpDataUserId)!.UserName;
                }

                //获取 用户对应的中间表 数据
                var UserRole = userroleRepository.GetAll().Where(s => s.UserId == item.Id && !s.IsDel).AsNoTracking(); ;
                // 获取 角色所有数据
                var role= roleRepository.GetAll().AsNoTracking();

                var quer = (from a in UserRole
                           join b in role
                           on a.RoleId equals b.Id
                           select b.RoleName).ToList();

                item.RoleName = string.Join('-', quer);
                //item.RoleName = GetRoleName(item.Id);



            }

            APIPaging<UserDTO> aPIPaging = new()
            {
                Code = EnumCode.Ok,
                Count = model.Count,
                PageData = model.OrderByDescending(s => s.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList()
            };

            return Task.FromResult(aPIPaging);




        }


        //public string GetRoleName (long Id)
        //{
        //    var res= userroleRepository.GetAll().Where(s=>s.UserId==Id).Select(s=>s.RoleId).ToList();

        //    string Name = "";
        //    foreach(var item in res)
        //    {


        //        var data = roleRepository.GetAll().FirstOrDefault(s => s.Id == item && !s.IsDel)?.RoleName;
        //        if(data==null)
        //        {
        //            Name +="";
        //        }
        //        else
        //        {
        //            Name += data+",";
        //        }
               



        //    }

        //    return Name.TrimEnd(',');
        //}
    }
}
